package com.chenbei.common.service.impl;

import com.chenbei.common.service.IEmailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class QQEmailService implements IEmailService {

    @Autowired(required = false)
    private JavaMailSender javaMailSender;

    @Autowired(required = false)
    private MailProperties mailProperties;

    @Override
    public void sendText(String to, String subject, String content) {
        if (javaMailSender != null && mailProperties != null) {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setTo(to);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(content);
            simpleMailMessage.setFrom(mailProperties.getUsername());
            try {
                javaMailSender.send(simpleMailMessage);
            } catch (MailException e) {
                log.error("邮箱发送异常，错误信息：{}", e.getMessage());
            }
        } else {
            log.error("请正确邮箱相关配置");
        }
    }

    @Override
    public void sendHtml(String to, String subject, String htmlContent) {
        //待实现
    }
}
